package com.xhd.db;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.cache.CacheMgr;
import com.xhd.cache.UseCache;
import com.xhd.exception.DatabaseException;
import com.xhd.server.ServerConfig;

public class XDBArea  implements UseCache
{
	  static HashMap jm564 = new HashMap();
	  static HashMap jm565 = new HashMap();
	  private HashMap jm566 = null;
	  private String jm328;
	  private static boolean jm158 = false;
	  private static final Log jm9;
	  static Class jm10;

	  static
	  {
	    Class tmp27_24 = jm10;
	    if (tmp27_24 == null)
	    {
	    
	      try
	      {
	        XDBArea.jm10 = Class.forName("com.sinocc.systemdbmanager.CPCDBArea");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp27_24);
	  }

	  public XDBArea(String paramString)
	  {
	    if (!(jm158))
	    {
	      jm158 = true;
	      CacheMgr.registerCache(this);
	    }
	    if (paramString == null)
	      paramString = "";
	    this.jm328 = paramString;
	    this.jm566 = ((HashMap)jm564.get(paramString));
	    if (this.jm566 != null)
	      return;
	    this.jm566 = new HashMap(64);
	    jm564.put(paramString, this.jm566);
	  }

	  public String getName()
	  {
	    return this.jm328;
	  }

	  protected BizObjFields getFields(String paramString, DBControl paramDBControl)
	    throws DatabaseException
	  {
	    BizObjFields localBizObjFields = (BizObjFields)this.jm566.get(paramString);
	    if (localBizObjFields == null)
	      try
	      {
	        localBizObjFields = paramDBControl.getTableFields(paramString);
	        if ((localBizObjFields != null) && (localBizObjFields.size() > 0))
	        {
	          this.jm566.put(paramString, localBizObjFields);
	          
	        }
	        jm9.error("Error: not found table " + paramString);
	      }
	      catch (DatabaseException localDatabaseException)
	      {
	        jm9.error(paramString, localDatabaseException);
	        throw new DatabaseException(localDatabaseException.getMessage());
	      }
	    label99: return localBizObjFields;
	  }

	  public static String getDBArea(String paramString1, String paramString2)
	  {
	    if (paramString2 == null)
	      return null;
	    paramString2 = paramString2.toLowerCase();
	    Map localMap = (Map)jm565.get(paramString2);
	    if ((localMap == null) && (paramString2.startsWith("tmp_")))
	    {
	      paramString2 = paramString2.substring(4);
	      int i = paramString2.lastIndexOf(95);
	      if ((i > 0) && (1 + i < paramString2.length()))
	      {
	        int j = paramString2.charAt(i + 1);
	        if ((j >= 48) && (j <= 57))
	          paramString2 = paramString2.substring(0, i);
	      }
	      localMap = (Map)jm565.get(paramString2);
	    }
	    if (localMap != null)
	    {
	      if ((localMap.size() == 1) || (paramString1 == null))
	        return ((String)localMap.values().iterator().next());
	      return ((String)localMap.get(paramString1));
	    }
	    return null;
	  }

	  public static XDBArea getDBAreaObj(String paramString1, String paramString2)
	  {
	    String str = getDBArea(paramString1, paramString2);
	    if (str != null)
	      return new XDBArea(str);
	    return new XDBArea("");
	  }

	  public static synchronized void initArea(DBConfig paramDBConfig)
	  {
	    if (!(jm158))
	    {
	      jm158 = true;
	      CacheMgr.registerCache(new XDBArea(""));
	    }
	    jm563(paramDBConfig, jm565);
	  }

	  public static void addTable(String paramString1, String paramString2, String paramString3)
	  {
	    String str = paramString3.toLowerCase();
	    HashMap localHashMap = (HashMap)jm565.get(str);
	    if (localHashMap == null)
	    {
	      localHashMap = new HashMap(1);
	      localHashMap.put(paramString1, paramString2);
	      jm565.put(str, localHashMap);
	    }
	    else
	    {
	      localHashMap.put(paramString1, paramString2);
	    }
	  }

	  private void jm562()
	  {
	    if (!(ServerConfig.UseDBArea))
	      return;
	    HashMap localHashMap1 = new HashMap(256);
	    for (int i = 0; i < ServerConfig.Vaults.size(); ++i)
	    {
	      DBConfig localDBConfig = new DBConfig();
	      if (!(localDBConfig.loadFromVault((Map)ServerConfig.Vaults.get(i))))
	        continue;
	      jm563(localDBConfig, localHashMap1);
	    }
	    HashMap localHashMap2 = jm565;
	    jm565 = localHashMap1;
	    localHashMap2.clear();
	    localHashMap2 = null;
	    localHashMap2 = jm564;
	    jm564 = new HashMap(8);
	    localHashMap2.clear();
	    localHashMap2 = null;
	  }

	  private static void jm563(DBConfig paramDBConfig, HashMap paramHashMap)
	  {
	    if ((paramDBConfig == null) || (paramDBConfig.getId() == null))
	      return;
	    String str1 = paramDBConfig.getArea();
	    if ((str1 == null) || (str1.length() < 1))
	      return;
	    String str2 = paramDBConfig.getAreaVaultId();
	    DBControl localDBControl = new DBControl(paramDBConfig);
	    int i = 0;
	    Vector localVector = null;
	    String str3 = paramDBConfig.getObjQuery();
	    try
	    {
	      localDBControl.getConn().setTransation(false);
	      if (str3 == null)
	      {
	        StringBuffer localStringBuffer;
	        if (paramDBConfig.getDBType() == 1)
	        {
	          localStringBuffer = new StringBuffer(128);
	          localStringBuffer.append("SELECT table_name FROM user_tables");
	          localStringBuffer.append(" ORDER BY table_name");
	          str3 = localStringBuffer.toString();
	        }
	        else
	        {
	          localStringBuffer = new StringBuffer(128);
	          localStringBuffer.append("SELECT name FROM sysobjects");
	          localStringBuffer.append(" WHERE uid=USER_ID('");
	          if ("sa".equals(paramDBConfig.getUser()))
	            localStringBuffer.append("dbo");
	          else
	            localStringBuffer.append(paramDBConfig.getUser());
	          localStringBuffer.append("')");
	          localStringBuffer.append(" AND OBJECTPROPERTY(id, N'IsUserTable')=1");
	          localStringBuffer.append(" ORDER BY name");
	          str3 = localStringBuffer.toString();
	        }
	      }
	      localVector = localDBControl.oneListSelect(str3, 0);
	      i = localVector.size();
	    }
	    catch (DatabaseException localDatabaseException)
	    {
	      jm9.error(localDatabaseException);
	    }
	    finally
	    {
	      if (localDBControl.isConnected())
	        localDBControl.freeConnection();
	    }
	    for (int j = 0; j < i; ++j)
	    {
	      String str4 = localVector.get(j).toString();
	      str4 = str4.toLowerCase();
	      HashMap localHashMap = (HashMap)paramHashMap.get(str4);
	      if (localHashMap == null)
	      {
	        localHashMap = new HashMap(1);
	        localHashMap.put(str2, str1);
	        paramHashMap.put(str4, localHashMap);
	      }
	      else
	      {
	        localHashMap.put(str2, str1);
	      }
	    }
	    jm9.info("Load " + str2 + " " + str1 + " table count: " + i);
	  }

	  public Object cacheData()
	  {
	    if (ServerConfig.UseDBArea)
	      return jm565;
	    return jm564;
	  }

	  public int cacheSize()
	  {
	    if (ServerConfig.UseDBArea)
	      return jm565.size();
	    return jm564.size();
	  }

	  public boolean clearCache()
	  {
	    if (4 == ServerConfig.SystemState)
	    {
	      jm565.clear();
	      jm564.clear();
	    }
	    else
	    {
	      jm562();
	    }
	    return true;
	  }
}
